<template>
  <div class="background">
           <a-alert
      v-if="visible"
      message="重置成功"
      :description="'新密码为'+password"
      type="success"
      show-icon
      closable
      :after-close="handleClose"
    />
    <a-card style="margin-bottom: 10px">

      <a-row>
        <a-col :span="24">
          <a-form
            layout="inline"
            :form="form"
            @submit="handleSubmit"
          >
            <a-form-item label="姓名">
              <a-input
                allow-clear
                placeholder="请输入姓名"
                v-model="name"
              ></a-input>
            </a-form-item>
            <a-form-item label="工号">
              <a-input
                allow-clear
                placeholder="请输入工号"
                v-model="jobNumber"
              ></a-input>
            </a-form-item>
            <a-form-item label="手机号">
              <a-input
                allow-clear
                placeholder="请输入手机号"
                v-model="mobile"
              ></a-input>
            </a-form-item>
            <a-form-item label="品牌">
              <a-select
                style="width: 180px"
                placeholder="请选择品牌"
                @change="selectChange"
                v-model="brand"
                allow-clear
              >
                <a-select-option
                  :value="item"
                  v-for="item in brandList"
                  :key="item"
                >
                  {{ item }}
                </a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item>
              <a-button
                type="primary"
                html-type="submit"
              >搜索</a-button>
              <a-button
                type="primary"
                style="margin-left: 10px"
                @click="add('')"
              >新增</a-button>
            </a-form-item>
          </a-form>
        </a-col>
      </a-row>
    </a-card>
    <a-card>
      <a-table
        bordered
        :loading="loading"
        :pagination="pagination"
        :columns="columns"
        :data-source="dataSource"
        @change="handleTableChange"
        :scroll="{ x: true }"
      >
        <span
          slot="brand"
          slot-scope="brand"
        >
          <a-tag
            v-for="(item,index) in brand"
            :key="index"
            :color="'blue'"
            style="margin-bottom:5px"
          >
            {{item}}
          </a-tag>
        </span>
        <span
          slot="permiss"
          slot-scope="permiss"
        >
          <a-tag
            v-for="(item,index) in permiss"
            :key="index"
            :color="item == '删款' ? 'volcano' :item == '主推' ? 'geekblue':'green'"
          >
            {{item}}
          </a-tag>
        </span>
        <template
          slot="action"
          slot-scope="txt, record"
        >
          <a
            class="mr-10"
            @click="add(record)"
          >
            <a-icon type="edit" />编辑
          </a>
          <a
            class="mr-10"
            @click="deleteRecord(record)"
          >
            <a-icon type="delete" />删除
          </a>
          <a
            class="mr-10"
            @click="passwordInit(record)"
          >
            <a-icon type="retweet" />密码重置
          </a>
        </template>
      </a-table>
      <create
        v-if="showAdd"
        :account="account"
        @close="close"
      ></create>
     
    </a-card>
  </div>
</template>

<script>
import create from './create.vue'
import { postAction } from '@/api/manage'
const columns = [
  // {
  //   title: '颜色编号',
  //   dataIndex: 'code',
  //   width: 200
  // },
  {
    title: '姓名',
    dataIndex: 'name',
    width: 80,
  },
  {
    title: '账号',
    dataIndex: 'account',
  },
  {
    title: '工号',
    dataIndex: 'jobNumber',
  },
  {
    title: '手机号',
    dataIndex: 'mobile',
  },
  {
    title: '角色',
    width: 90,
    dataIndex: 'roleName',
  },
  {
    title: '品牌',
    key: 'brand',
    dataIndex: 'brand',
    scopedSlots: { customRender: 'brand' },
  },
  {
    title: '权限',
    key: 'permiss',
    dataIndex: 'permiss',
    width: 180,
    scopedSlots: { customRender: 'permiss' },
  },
  {
    title: '操作',
    width: 200,
    scopedSlots: { customRender: 'action' },
  },
]
const brandList = ['JNBY', 'CROQUIS', '童装', '婴童', 'LESS', '蓬马', 'APN 73', 'JNBY HOME']
export default {
  components: {
    create,
  },
  data() {
    return {
      columns,
      form: this.$form.createForm(this),
      jobNumber: '',
      showAdd: false,
      visible: false,
      password:'',
      patt: 'api/gateway/basemgt-op',
      account: '',
      name: '',
      loading: true,
      mobile: '',
      dataSource: [],
      brand: '',
      brandList,
      brands: [],
      permiss: [],
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30', '40'],
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total) => `共 ${total} 条记录`,
        itemRender: (current, type, originalElement) => {
          if (type === 'prev') {
            return <a>上一页</a>
          } else if (type === 'next') {
            return <a>下一页</a>
          }
          return originalElement
        },
      },
    }
  },
  mounted() {
    this.loadData()
    // this.getBrandList();
  },

  methods: {
    handleTableChange(pagination) {
      this.pagination = pagination
      this.loadData(1)
    },
    close() {
      this.showAdd = false
      this.loadData(1)
    },
    add(record) {
      this.showAdd = true
      this.account = record
    },
    update(color) {
      this.color = color
      this.showUpdate = true
    },
    deleteRecord(record) {
      this.$confirm({
        content: '确定要删除' + record.name + '的账号吗？',
        onOk: () => {
          const data = {
            accountId: record.id,
          }
          postAction(`${this.patt}/account/delete`, data).then((data) => {
            if (data.code == 200) {
              this.$message.success('删除成功')
              this.loadData()
            } else {
              this.$message.warning(data.msg)
            }
          })
        },
        onCancel() {},
      })
    },
    passwordInit(record) {
      this.$confirm({
        content: '确定要重置' + record.name + '的密码吗？',
        onOk: () => {
          const data = {
            jobNumber: record.jobNumber,
          }
          postAction(`${this.patt}/account/password/init`, data).then((data) => {
            if (data.code == 200) {
              this.visible = true
              this.password = data.data.password
            } else {
              this.$message.warning(data.msg)
            }
          })
        },
        onCancel() {},
      })
    },
     handleClose() {
      this.visible = false;
    },
    selectChange(brand) {
      this.brand = brand
    },
    handleSubmit(e) {
      e.preventDefault()
      this.loadData()
    },
    loadData(key) {
      const params = {}
      if (key) {
        params.current = this.pagination.current
        params.size = this.pagination.pageSize
      } else {
        params.current = 1
      }
      if (this.brand) {
        params.brand = this.brand
      }
      if (this.name) {
        params.name = this.name
      }
      if (this.jobNumber) {
        params.jobNumber = this.jobNumber
      }
      if (this.mobile) {
        params.mobile = this.mobile
      }
      try {
        postAction(`${this.patt}/account/list`, params).then((data) => {
          if (data.code == 200) {
            this.dataSource = data.data.records
            this.pagination.current = data.data.current
            this.pagination.total = data.data.total
            this.dataSource.forEach(function (item) {
              item.brand = item.brand.split(',')
            })
            this.dataSource.forEach(function (item) {
              item.permiss = []
              if (item.mainDesigner == 1) {
                item.permiss.push('主推')
              }
              if (item.chooseStyle == 1) {
                item.permiss.push('选款')
              }
              if (item.deleteStyle == 1) {
                item.permiss.push('删款')
              }
            })
          } else {
            this.$message.warning(data.msg)
          }
        })
      } catch (error) {
      } finally {
        this.loading = false
      }
    },
  },
}
</script>
<style lang="less" scoped>
.background {
  background: transparent;
}
.ant-modal-body {
  display: flex;
  justify-content: space-between;
}
</style>